Introduction
In this simple article I'd like to show two
different ways how you can pass control values
(i.e. TextBox) from one Windows Form to a second Windows Form.
Actually I never worried about this issue until I also had
to struggle to find a solution myself in a C#-project.
Although from time to time I am still reading on .NET Forums that
people are always asking always the same question: i.e.:
please help, how can I pass a TextBox value or a
variable from a Form to a second Form etc..
Of
course, there were some interesting ways to do it.
Anyway,
in terms of this issue I have also 2 different ways/solutions which
I would like to share with you and which I think might be the
interesting solutions to those questions on the .NET forums.
As you know, Visual Studio.NET automatically generates a solution
when you create a new project. There is as you will see for this
project one solution "PassingValuesFromFormToForm" containing 2
different projects.
Project
"ProjectPassValuesFromFormToForm_1Way" is for the first way, project
"ProjectPassValuesFromFormToForm_2Way" is for the second way.
First Way, how to
pass TextBox values from one Form to Another?
For this reason I use 2 TextBoxes as input on the Form1.

If you click the "GoToForm2" button
the values of these 2 TextBoxes will appear on Form2.

Form1.cs in project
"PassingValuesFromFormToForm_1"
If you use Visual Studio.NET and drag/drop the 2 TextBoxes on the
Form, access modifier of these 2 TextBoxes in Form1.cs will be
private by default. It is therefore very important here you change
the modifier private to public because otherwise these 2 TextBoxes
would be inaccessible in Form2.cs.
Form1.cs
public class clsForm1 :
System.Windows.Forms.Form
{
public
System.Windows.Forms.TextBox textBox1InForm1;
public
System.Windows.Forms.TextBox textBox2InForm1;
...
...
}
As a reminder I'd
like to make mention of access modifiers briefly used to specify the
declared accessibility of a member or a type in OO-Programming.
- private - Private members are accessible only within the body
of the class or the struct in which they are declared. Access is
limited to the containing type.
- public - Public access is the most permissive access level.
You have no restrictions on accessing public members.
- protected - A protected member of a base class can be accessed
in a derived class only if the access takes place through the
derived class type.
- internal - Internal members are accessible only within files
in the same assembly.
- protected internal - Protected internal access is limited to
the current assembly or types derived from the containing
class.
More on: MSDN
Now, after entering some text in 2 TextBoxes you click the "Go to
Form2" button. The code within the event handler for the
btGoToForm2_Click which is necessary to open/show the Form2 is as
follows:
private void btGoToForm2_Click(object sender, System.EventArgs
e)
{
//instantiate frm2 as
local
clsForm2 frm2 = new
clsForm2();
//frm1inForm2 is the instance of
the class clsForm1 in Form2.cs declared as public
frm2.frm1inForm2 = this;
//Display the Form2 as a modal
dialog box.
frm2.ShowDialog();
}
I think the code is self-explanatory. The only additional thing
to say here is not to forget to change the modifier private to
public of the instance of the class clsForm1 "frm1inForm2" in
Form2.cs. Otherwise you get the compile-error like this:
'PassingDataFromForm1ToForm2.clsForm2.frm1inForm2' is
inaccessible due to its protection level
Now we look at Form2.cs
Form2 looks like
Form1 containing 2 TextBoxes because we want to display the values
of 2 TextBoxes from Form1 on Form2 in 2 new TextBoxes. Here in
Form2.cs we keep the access modifier private for 2 TextBoxes and
declare the instance of the class clsForm1 "frm1inForm2" in Form2.cs
as public because we want to access to the TextBoxes on Form1 .
public class clsForm2 :
System.Windows.Forms.Form
{
private
System.Windows.Forms.TextBox textBox1_InForm2;
private
System.Windows.Forms.TextBox textBox2_InForm2;
...
...
public clsForm1 frm1inForm2;
...
...
}
If the Form2 is loading the following code will be executed which
is I think the most important part in this application.
private void Form2_Load(object sender, System.EventArgs
e)
{
this.textBox1_InForm2.Text =
((clsForm1)this.frm1inForm2).textBox1InForm1.Text;
this.textBox2_InForm2.Text =
((clsForm1)this.frm1inForm2).textBox2InForm1.Text;
}
clsForm1 : Class name in Form1.cs
frm1inForm2 : public object
instaniation of clsForm1 in Form2.cs
It assigns the 2
TextBoxes "textBox1Form1" and "textBox2InForm1" of the Form1 to the
2 TextBoxes(textBox1_InForm2, textBox2_InForm2) on Form2 combined
with "(clsForm1)this.frm1inForm2)" you can now have access to the
control values on Form1 from the Form2.
Second Way, how to
pass TextBox values to a Second Form?
For the second
way I make use of passing parameter to constructor.
A
constructor can generalize for example the initialization, assigning
start values of an object. It is like using a method. As you know
all classes must have at least one constructor, which initializes
new instances of the class. An instance of a class is an object. You
access an object's functionality by calling its methods and
accessing its properties, events, and fields.
A default
constructor is automatically provided by Visual Studio.NET like the
following:
default constructor provided by Visual
Studio.NET
public class Form2 :
System.Windows.Forms.Form
{
//variables, constants
etc.
...
...
public
Form2()
{
//
// Required for Windows Form
Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code
after InitializeComponent call
//
}
...
...
}
constructor with string parameter
public class Form2 :
System.Windows.Forms.Form
{
//variables, constants
etc.
...
...
public Form2(string strParam)
{
InitializeComponent();
this.textBox1_InForm2.Text=strParam;
}
...
...
}
This is the new constructor with a string parameter you have to
create/add and which is used for the second way.
Now back to the Forms for the second projects
"ProjectPassValuesFromFormToForm_2Way".
On Form1 I use only 1
TextBox which is used as parameter passed to the constructor of the
Form2.

As usual if you click the button "Go to Form2" on the Form1 the
text entered in the TextBox on the Form1 is displayed on the Form2.
Form1.cs
You enter some text in the one TextBox on the Form1 and click the
"Go to Form2" button. The code within the event handler for the
btGoToForm2_Click is as follows:
private void btGoToForm2_Click(object sender, System.EventArgs
e)
{
Form2 frm2 = new Form2(this.textBox1InForm1.Text);
frm2.ShowDialog();
}
Form2 frm2 = new Form2(this.textBox1InForm1.Text);
frm2 is
the instance of the class Form2 in Form2.cs you created with the new
operator. And pass the TextBox value as parameter which will be used
in the constructor of the class
Form2.
frm2.ShowDialog();
It shows the Form2 as a modal
dialog with the specified owner.
Form2.cs
public class Form2 :
System.Windows.Forms.Form
{
//variables, constants
etc.
..
..
//default
constructor
public
Form2()
{
//
// Required for Windows Form
Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code
after InitializeComponent call
//
}
..
..
// the new added constructor with
parameter
public Form2(string strParam)
{
InitializeComponent();
this.textBox1_InForm2.Text=strParam;
}
..
..
}
In Form2.cs, you need only to add a new constructor with a
parameter which is in this case a string and
assign this
parameter to the TextBox(textBox1_InForm2) on Form2 and you got
it.
Conclusion
I hope that you can make use of these solutions above and that I
showed in this project something useful.
Happy coding.